from driver_school_after.utils import is_blank
from rest_framework.decorators import api_view
from login.models import Car_info, Coach_info
from django.http import JsonResponse
from django.core.paginator import Paginator


# Create your views here.
@api_view(["get"])
def cars(request):
    page = request.GET.get('page')
    count = request.GET.get('count')
    car = Car_info.objects.values()
    total = car.count()
    if is_blank(page) or int(page) < 1:
        page = 1
    if is_blank(count):
        count = total
    pageobject = Paginator(car, count)
    pageobject_data = pageobject.page(page)
    data = []
    for item in pageobject_data.object_list:
        item["coach"] = Coach_info.objects.filter(coach_id=item["coa_id"]).values().first()
        data.append(item)

    return JsonResponse({"status": 200, "message": "查询成功", "data": data, "total": total})
